package com.xq.rabbitmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;

import java.nio.charset.StandardCharsets;
import java.util.UUID;

/**
 * 功能说明:消息接收者.
 * <p>创建时间:2018-12-31 08:49</p>
 *
 * @author <a href="https://gitee.com/gkq/microservice">郭凯强</a>
 * @since 1.0.0
 */
public class Recv {
    private static final String QUEUE_NAME = "hello";
    private static final Logger LOGGER = LoggerFactory.getLogger(Recv.class);
    private static final Marker MARKER = MarkerFactory.getMarker("Recv");
    private static final String uniqKey=UUID.randomUUID().toString();

    public static void main(String[] args) throws Exception {
        LOGGER.debug(MARKER, "Recv {} 准备阶段...", uniqKey);
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.1.77");
        factory.setUsername("admin");
        factory.setPassword("gkq1986");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        LOGGER.debug(MARKER, "[*] Waiting for message. To exit press {}", "CTRL+C");

        DeliverCallback deliverCallback = (consumerTag, message) -> {
            String receivedMsg = new String(message.getBody(), StandardCharsets.UTF_8);
            LOGGER.debug(MARKER, "consumerTag={} Received {}", consumerTag, receivedMsg);
        };

        channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> {
        });
    }
}
